package com.codejiwei.core.rdd

import org.apache.spark.{SparkConf, SparkContext}

object Spark_Operator_MapPartitionsWithIndex {
  def main(args: Array[String]): Unit = {
    val conf = new SparkConf().setMaster("local[1]").setAppName("MapPartitionsWithIndex")
    val sc = new SparkContext(conf)

    val rdd = sc.makeRDD(List(1, 2, 3, 4, 5), 2)

    //以分区为单位，给每个分区建立索引值Index（可以根据索引值来过滤某一个分区的值）
    //以分区为单位，给每个分区建立索引值index
    val rdd1 = rdd.mapPartitionsWithIndex {
      case (index, iter) => {
        if (index == 1) {
          iter
        } else {
          Nil.iterator
        }
      }
    }

    rdd1.collect().foreach(println)

    sc.stop()
  }
}
